package com.lincoln.tb.spider.util;

import com.lincoln.tb.spider.bean.Cat;
import com.lincoln.tb.spider.bean.Good;
import com.lincoln.tb.spider.bean.Type;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 店铺爬取工具类
 *
 * @author lincoln
 */
public class ShopUtil {
    /**
     * 校验url类型
     *
     * @param url
     * @return
     */
    public static Type switchType(String url) {
        if ((url.startsWith("https://shop") || url.startsWith("shop")) && (url.indexOf(".taobao.com") != -1)) {
            return Type.TB;
        }
        return Type.NULL;
    }

    public static Cat spiderCats(String url) {
        return null;
    }

    /**
     * 爬取淘宝的分类
     *
     * @param url
     * @return
     */
    public static Cat spiderTbCats(String url) throws IOException {
        Document document = Jsoup.parse(new URL(url), 10000);
        Element fstCatElement = document.getElementsByClass("cat-name fst-cat-name").first();
        Cat fstCat = new Cat();
        fstCat.setType(Type.TB);
        fstCat.setCatName(fstCatElement.html());
        Elements catElements = document.select("li[class=cat fst-cat]");
        for (int i = 1; i < catElements.size(); i++) {
            Cat cat = new Cat();
            cat.setType(Type.TB);
            cat.setParent(fstCat);
            Element a = catElements.get(i).getElementsByTag("a").first();
            cat.setCatId(parseCatId(a.attr("href")));
            cat.setCatName(a.html());
            //查询所有的子分类
            Elements sndCatElements = catElements.get(i).select("li[class=cat snd-cat]");
            //如果有子分类
            if (sndCatElements != null && sndCatElements.size() > 0) {
                for (int j = 0; j < sndCatElements.size(); j++) {
                    Cat sndCat = new Cat();
                    sndCat.setParent(cat);
                    sndCat.setType(Type.TB);
                    Element sndA = sndCatElements.get(j).getElementsByTag("a").first();
                    sndCat.setCatId(parseCatId(sndA.attr("href")));
                    sndCat.setCatName(sndA.html());
                    sndCat.setGoods(spiderGoods("https:" + sndA.attr("href").substring(0, sndA.attr("href").indexOf(".htm")) + ".htm"));
                    cat.getChildren().add(sndCat);
                }
            } else {
                cat.setGoods(spiderGoods("https:" + a.attr("href").substring(0, a.attr("href").indexOf(".htm")) + ".htm"));
            }
            fstCat.getChildren().add(cat);
        }
        return null;
    }

    public static String buildSpiderGoodsCookies() {
        return "t=0a08949889bb4012ac3d3b47a374aaa6; v=0; _tb_token_=510577ee186eb; thw=cn; sg=19c; _l_g_=Ug%3D%3D; csg=8d55319f; existShop=MTU1NDg3OTk4MA%3D%3D; tracknick=lincolnking1; lgc=lincolnking1; _cc_=VT5L2FSpdA%3D%3D; dnk=lincolnking1; _nk_=lincolnking1; tg=0; cna=pnU0FWdHH2cCAdpoi3S1Dx3A; mt=ci=45_1; swfstore=264951; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; uc1=cookie14=UoTZ4Mc1mdab7w%3D%3D&lng=zh_CN&cookie16=URm48syIJ1yk0MX2J7mAAEhTuw%3D%3D&existShop=false&cookie21=UtASsssmeW6lpyd%2BBROh&tag=8&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0; pnm_cku822=098%23E1hvN9vUvbpvUpCkvvvvvjiPRLF9gj3PRsdvljnEPmPU1jlbPFMylj3En2cW1jYWnvhCvvOv9hCvvvmEvpvVvpCmpYsvuphvmvvvpo%2BObZwJKphv8vvvpLIvvUm5vvChQvvvvo%2BvvhNjvvvmjvvvBGwvvvUUvvCh6vvvvawEvpCWvRRzr10veEyaRoxBlwyzhb8rwyalYb8reTgfNezhj8TKHmx%2FAWoKDC46NB3rAEuK51izprsIVt94V0Q4S4ZAhCkaU6bnDBmOV36AxQGCvvLMMQvvRphvCvvvphm5vpvhvvCCBv%3D%3D; l=bBOv97vPvnbLTjjsBOCwIZtVce_TbIRAguWXgPc2i_5Cx6L1pY7OlMG_1Fp6Vj5R_QYB4WFx9gp9-etus; isg=BKqqANipgsj0yw6k43B7pyHB-xBGayLgEZNJijRjSv2IZ0ohHKtghNsd89Nel6YN; whl=-1%260%260%261554883887885";
    }

    /**
     * 爬取商品
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static List<Good> spiderGoods(String url) throws IOException {
        return spiderGoods2(spiderDataUrl(url));
    }

    public static String spiderDataUrl(String url) throws IOException {
        Document document = Jsoup.parse(new URL(url), 10000);
        String dataUrl = url.substring(0, url.indexOf(".com") + ".com".length()) + document.getElementById("J_ShopAsynSearchURL").val();
        return dataUrl;
    }

    public static List<Good> spiderGoods2(String url) throws IOException {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Document document = Jsoup.connect(url).header("cookie", buildSpiderGoodsCookies()).get();
        Elements goodElements = document.select("a[class=\\\"J_TGoldData\\\"]");
        List<Good> goods = new ArrayList<>();
        for (int i = 0; i < goodElements.size(); i++) {
            String goodUrl = "https:" + goodElements.get(i).attr("href").substring(2, goodElements.get(i).attr("href").length() - 2);
            Good good = new Good();
            good.setType(Type.TB);
            good.setGoodId(parseGoodId(goodUrl));
            good.setPics(Utils.spiderTbPic(goodUrl));
            goods.add(good);
        }
        try {
            Element nextPage = document.selectFirst("div[class=\\\"pagination\\\"]").getElementsByTag("a").last();
            if ("".equals(nextPage.attr("href"))) {
                return goods;
            } else {
                goods.addAll(spiderGoods2(spiderDataUrl(url.substring(0, url.indexOf(".com") + ".com".length()) + "///%22" + nextPage.attr("href").substring(2))));
                return goods;
            }
        }catch (NullPointerException e){
            //这里被阿里封了
            System.out.println("被封了");
            return goods;
        }
    }

    /**
     * 提取商品id
     *
     * @param url
     * @return
     */
    public static String parseGoodId(String url) {
        return url.substring(url.indexOf("id="));
    }

    /**
     * 解析分类url中的分类id
     *
     * @param url
     * @return
     */
    public static String parseCatId(String url) {////shop120820332.taobao.com/category-1071274605.htm?search=y&amp;catName=Nike
        int start = url.indexOf("category-") + "category-".length();
        if (start == -1) {
            return "";
        }
        int end = url.indexOf(".htm", start);
        return url.substring(start, end);
    }

    public static void main(String[] args) throws IOException {
//        spiderTbCats("https://shop120820332.taobao.com/?spm=2013.1.1000126.2.13946f35vi4PxX");
        spiderTbCats("https://shop60374631.taobao.com/?spm=a230r.7195193.1997079397.2.4a9673f0pESB5j");
//        spiderGoods("https://7two.taobao.com/category-1426067787.htm");
    }
}
